// pages/shop/shop.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        gifts: ''

    },
    /**
     * 兑换礼物
     */
    exchangeGift(event) {
        let user = wx.getStorageSync('user')
        if (user) {
            let gift = event.currentTarget.dataset
            let integral = gift.gift.gift_integral
            let gift_name = gift.gift.gift_content
            let task_type = 'exchange rewards'
            wx.showModal({
                title: '提示',
                content: '已选择兑换：'+ gift_name +'？'+'扣除'+ integral +'分',
                success: res => {
                    if (res.cancel) {
                    } else {
                        // this.getUserIntegral(user.nickName, integral)
                        // console.log(integral)
                        // 查询用户可用积分
                        wx.cloud.callFunction({
                            name: 'babyCloudFuncs',
                            data: {
                                type: 'getUserInfo',
                                data: {
                                    nickName: user.nickName
                                }
                            }
                        }).then((res) => {
                            let tmp = res.result.data
                            if (tmp.length != 0) {
                                let has_integral = tmp[0].has_integral
                                if (has_integral < integral) {
                                    wx.showModal({
                                        title: '警告',
                                        content: '你可用积分不足以 购买' + gift_name + ' 请努力赚取积分QAQ',   
                                        showCancel: false 
                                    })
                                } else {
                                    wx.showToast({
                                        title: '已扣除'+ integral +'积分',
                                        icon: 'success',
                                        duration: 1000,
                                    })
                                    // 更新用户信息
                                    wx.cloud.callFunction({
                                        name: 'babyCloudFuncs',
                                        data: {
                                            type: 'updateUserInfo',
                                            data: {
                                                    nickName: user.nickName,
                                                    getIntegral: 0,
                                                    useIntegral: integral,
                                                    clockDays: 0,
                                            }
                                        }
                                    })
                                    // 记录用户操作记录-兑换奖励
                                    wx.cloud.callFunction({
                                        name: 'babyCloudFuncs',
                                        data: {
                                            type: 'operationRecord',
                                            data: {
                                                    nickName: user.nickName,
                                                    taskName: gift_name,
                                                    taskType: task_type,
                                                    taskIntegral: integral
                                                }
                                            }
                                    }) 
                                    console.log('====')
                                }
                            }  
                        })
                    }
                }
              })
        } else {
            wx.showModal({
                title: '提示',
                content: '请先登录，才能完成相关操作',
                showCancel: false
              })
        }
    },
    /**
     * 获取用户的所有奖励
     */
    getUserAllGifts () {
        let user = wx.getStorageSync('user')
        if (user) {
            wx.cloud.callFunction({
                name: 'babyCloudFuncs',
                data: {
                    type: 'showUserGift',
                    data: {
                        nickName: user.nickName
                    }
                }
            }).then((res) => {
                this.setData({
                    gifts: res.result.data
                })
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserAllGifts()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})